
package com.food.mall.controller.mall;

import com.food.mall.common.Constants;
import com.food.mall.common.IndexConfigTypeEnum;
import com.food.mall.common.FoodMallException;
import com.food.mall.controller.vo.FoodMallIndexCarouselVO;
import com.food.mall.controller.vo.FoodMallIndexCategoryVO;
import com.food.mall.controller.vo.FoodMallIndexConfigGoodsVO;
import com.food.mall.service.FoodMallCarouselService;
import com.food.mall.service.FoodMallCategoryService;
import com.food.mall.service.FoodMallIndexConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private FoodMallCarouselService foodMallCarouselService;

    @Resource
    private FoodMallIndexConfigService foodMallIndexConfigService;

    @Resource
    private FoodMallCategoryService foodMallCategoryService;

    @GetMapping({"/index", "/", "/index.html"})
    public String indexPage(HttpServletRequest request) {
        List<FoodMallIndexCategoryVO> categories = foodMallCategoryService.getCategoriesForIndex();
        if (CollectionUtils.isEmpty(categories)) {
            FoodMallException.fail("分类数据不完善");
        }
        List<FoodMallIndexCarouselVO> carousels = foodMallCarouselService.getCarouselsForIndex(Constants.INDEX_CAROUSEL_NUMBER);
        List<FoodMallIndexConfigGoodsVO> hotGoodses = foodMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_HOT.getType(), Constants.INDEX_GOODS_HOT_NUMBER);
        List<FoodMallIndexConfigGoodsVO> newGoodses = foodMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_NEW.getType(), Constants.INDEX_GOODS_NEW_NUMBER);
        List<FoodMallIndexConfigGoodsVO> recommendGoodses = foodMallIndexConfigService.getConfigGoodsesForIndex(IndexConfigTypeEnum.INDEX_GOODS_RECOMMOND.getType(), Constants.INDEX_GOODS_RECOMMOND_NUMBER);
        request.setAttribute("categories", categories);//分类数据
        request.setAttribute("carousels", carousels);//轮播图
        request.setAttribute("hotGoodses", hotGoodses);//热销商品
        request.setAttribute("newGoodses", newGoodses);//新品
        request.setAttribute("recommendGoodses", recommendGoodses);//推荐商品
        return "mall/index";
    }
}
